package test.threads.queue.common;

import java.util.Locale;
import java.util.concurrent.Executor;

import com.lmax.disruptor.EventFactory;
import net.jcip.annotations.Immutable;

/**
 * @author cheremin
 * @since 27.02.12,  14:06
 */
public interface ITask<E> {

    public int workersRequired();

    public void initialize( final Executor threadPool,
                            final IUsefulWorkEmulator<E>[] emulators,
                            final EventFactory<E> eventFactory );

    public BenchmarkResult[] execute( final long millisToBogus,
                                    final long millisToMeasure,
                                    final int turns );

    public void terminate();

    @Immutable
    public static class BenchmarkResult {
        public final long timeEllapsedMilliseconds;
        public final long timeEllapsedNanoseconds;

        public final long packetsCompleted;

        public BenchmarkResult( final long timeEllapsedMilliseconds,
                                final long timeEllapsedNanoseconds,
                                final long packetsCompleted ) {
            this.timeEllapsedMilliseconds = timeEllapsedMilliseconds;
            this.timeEllapsedNanoseconds = timeEllapsedNanoseconds;
            this.packetsCompleted = packetsCompleted;
        }

        public String print( final String taskName,
                             final String payload ) {
            return String.format(
                    Locale.US,
                    "%s@%s: %d packets of %s, takes %d ms (%.1f packets/ms)",
                    taskName,
                    payload,
                    packetsCompleted, payload,
                    timeEllapsedMilliseconds,
                    packetsCompleted * 1.0 / timeEllapsedMilliseconds

            );
        }

        public String printTabular( final String taskName,
                                    final String payloadDescription ) {
            return String.format(
                    Locale.US,
                    "%s@%s\t%d\t%d\t%.1f",
                    taskName,
                    payloadDescription,
                    packetsCompleted,
                    timeEllapsedMilliseconds,
                    packetsCompleted * 1.0 / timeEllapsedMilliseconds

            );
        }
    }
}
